package shemes;

import java.awt.Point;
import java.util.ArrayList;

import gui.*;
import components.*;

public class Intr3 {

    private static CD CD1, CD2;
    private static BoolsToInt UINT, UEXT;
    private static TSB TSBUINTout1, TSBUEXTout1;
    private static IntToInt IBUS1low;
    private static REG BR;
    private static IntToBools BRbits;
    private static BoolsToInt IVTDSP;
    private static TSB TSBIVTDSPout2;
    private static REG IVTP;
    private static TSB TSBIVTPout1;
    private static GuiScheme gui;

    public static GuiScheme getGui() {
        return gui;
    }

    public static void init() {
        CD1 = new CD(4);
        CD2 = new CD(8);

        UINT = new BoolsToInt(8, 8);
        UEXT = new BoolsToInt(8, 8);

        TSBUINTout1 = new TSB();
        TSBUINTout1.getOut(0).setNumOfLines(16);
        TSBUEXTout1 = new TSB();
        TSBUEXTout1.getOut(0).setNumOfLines(16);

        IBUS1low = new IntToInt(16, 8);

        BR = new REG(1, "BR");
        BR.getOut(0).setIsInt();
        BR.getOut(0).setNumOfLines(8);

        BRbits = new IntToBools(8, 8);
        IVTDSP = new BoolsToInt(16, 16);
        TSBIVTDSPout2 = new TSB();
        TSBIVTDSPout2.getOut(0).setNumOfLines(16);

        IVTP = new REG(1, "IVTP");
        IVTP.getOut(0).setIsInt();
        IVTP.getOut(0).setNumOfLines(16);

        TSBIVTPout1 = new TSB();
        TSBIVTPout1.getOut(0).setNumOfLines(16);

    }

    public static void connect() {

        CD1.setE(new Pin(true));
        CD1.setInputPin(0, Exec2.PSWT());
        CD1.setInputPin(1, Intr1.PRINM());
        CD1.setInputPin(2, Intr1.PRADR());
        CD1.setInputPin(3, Intr1.PRCOD());

        UINT.setInputPin(0, CD1.getOut(0));
        UINT.setInputPin(1, CD1.getOut(1));
        for (int i = 2; i < 8; i++) {
            UINT.setInputPin(i, new Pin(false));
        }

        TSBUINTout1.setInputPin(0, UINT.getOut(0));
        TSBUINTout1.setCtrl(Oper1.UINTout1());
        Bus1.addOnIBUS1(TSBUINTout1.getOut(0));

        CD2.setE(new Pin(true));
        CD2.setInputPin(0, new Pin(false));
        CD2.setInputPin(1, Intr2.irm1());
        CD2.setInputPin(2, Intr2.irm2());
        CD2.setInputPin(3, Intr2.irm3());
        CD2.setInputPin(4, Intr2.irm4());
        CD2.setInputPin(5, Intr2.irm5());
        CD2.setInputPin(6, Intr2.irm6());
        CD2.setInputPin(7, Intr2.irm7());

        UEXT.setInputPin(0, CD2.getOut(0));
        UEXT.setInputPin(1, CD2.getOut(1));
        UEXT.setInputPin(2, CD2.getOut(2));
        UEXT.setInputPin(3, new Pin(true));
        for (int i = 4; i < 8; i++) {
            UEXT.setInputPin(i, new Pin(false));
        }

        TSBUEXTout1.setInputPin(0, UEXT.getOut(0));
        TSBUEXTout1.setCtrl(Oper1.UEXTout1());
        Bus1.addOnIBUS1(TSBUEXTout1.getOut(0));

        IBUS1low.setInputPin(0, Bus1.IBUS1());
        BR.setInputPin(0, IBUS1low.getOut(0));
        BR.setPinLd(Oper1.ldBR());
        BRbits.setInputPin(0, BR.getOut(0));

        IVTDSP.setInputPin(0, new Pin(false));
        for (int i = 0; i < 8; i++) {
            IVTDSP.setInputPin(1 + i, BRbits.getOut(i));
        }
        for (int i = 9; i < 16; i++) {
            IVTDSP.setInputPin(i, new Pin(false));
        }

        TSBIVTDSPout2.setInputPin(0, IVTDSP.getOut(0));
        TSBIVTDSPout2.setCtrl(Oper1.IVTDSPout2());
        Bus1.addOnIBUS2(TSBIVTDSPout2.getOut(0));

        IVTP.setInputPin(0, Bus1.IBUS2());
        IVTP.setPinLd(Oper1.ldIVTP());

        TSBIVTPout1.setInputPin(0, IVTP.getOut(0));
        TSBIVTPout1.setCtrl(Oper1.IVTPout1());
        Bus1.addOnIBUS1(TSBIVTPout1.getOut(0));

    }

    public static void gui() {
        GuiLine line; // Pomocna promenljiva
        gui = new GuiScheme("src/images/Intr3.png");

        ArrayList<ArrayList<Point>> sections;
        ArrayList<Point> points;


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(105, 10));
        points.add(new Point(105, 733));
        sections.add(points);
        points = new ArrayList<Point>();
        points.add(new Point(107, 10));
        points.add(new Point(107, 733));
        sections.add(points);
        line = new GuiLine(sections, Bus1.IBUS1());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(542, 12));
        points.add(new Point(542, 733));
        sections.add(points);
        points = new ArrayList<Point>();
        points.add(new Point(544, 12));
        points.add(new Point(544, 733));
        sections.add(points);
        line = new GuiLine(sections, Bus1.IBUS2());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(196, 72));
        points.add(new Point(204, 72));
        sections.add(points);
        line = new GuiLine(sections, Intr1.PRCOD());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(196, 87));
        points.add(new Point(204, 87));
        sections.add(points);
        line = new GuiLine(sections, Intr1.PRADR());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(196, 102));
        points.add(new Point(204, 102));
        sections.add(points);
        line = new GuiLine(sections, Intr1.PRINM());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(196, 117));
        points.add(new Point(204, 117));
        sections.add(points);
        line = new GuiLine(sections, Exec2.PSWT());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(271, 27));
        points.add(new Point(279, 27));
        sections.add(points);
        line = new GuiLine(sections, new Pin(false));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(271, 57));
        points.add(new Point(279, 57));
        sections.add(points);
        line = new GuiLine(sections, new Pin(false));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(271, 72));
        points.add(new Point(279, 72));
        sections.add(points);
        line = new GuiLine(sections, new Pin(false));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(249, 87));
        points.add(new Point(279, 87));
        sections.add(points);
        line = new GuiLine(sections, CD1.getOut(1));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(249, 102));
        points.add(new Point(279, 102));
        sections.add(points);
        line = new GuiLine(sections, CD1.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(300, 64));
        points.add(new Point(357, 64));
        points.add(new Point(357, 151));
        points.add(new Point(202, 151));
        sections.add(points);
        line = new GuiLine(sections, UINT.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(191, 162));
        points.add(new Point(191, 155));
        sections.add(points);
        line = new GuiLine(sections, Oper1.UINTout1());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(181, 150));
        points.add(new Point(108, 150));
        sections.add(points);
        line = new GuiLine(sections, TSBUINTout1.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(196, 311));
        points.add(new Point(204, 311));
        sections.add(points);
        line = new GuiLine(sections, new Pin(false));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(196, 296));
        points.add(new Point(204, 296));
        sections.add(points);
        line = new GuiLine(sections, Intr2.irm1());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(196, 281));
        points.add(new Point(204, 281));
        sections.add(points);
        line = new GuiLine(sections, Intr2.irm2());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(196, 266));
        points.add(new Point(204, 266));
        sections.add(points);
        line = new GuiLine(sections, Intr2.irm3());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(196, 251));
        points.add(new Point(204, 251));
        sections.add(points);
        line = new GuiLine(sections, Intr2.irm4());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(196, 237));
        points.add(new Point(204, 237));
        sections.add(points);
        line = new GuiLine(sections, Intr2.irm5());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(196, 222));
        points.add(new Point(204, 222));
        sections.add(points);
        line = new GuiLine(sections, Intr2.irm6());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(196, 207));
        points.add(new Point(204, 207));
        sections.add(points);
        line = new GuiLine(sections, Intr2.irm7());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(271, 199));
        points.add(new Point(279, 199));
        sections.add(points);
        line = new GuiLine(sections, new Pin(false));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(271, 229));
        points.add(new Point(279, 229));
        sections.add(points);
        line = new GuiLine(sections, new Pin(true));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(249, 244));
        points.add(new Point(279, 244));
        sections.add(points);
        line = new GuiLine(sections, CD2.getOut(2));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(249, 259));
        points.add(new Point(279, 259));
        sections.add(points);
        line = new GuiLine(sections, CD2.getOut(1));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(249, 274));
        points.add(new Point(279, 274));
        sections.add(points);
        line = new GuiLine(sections, CD2.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(300, 237));
        points.add(new Point(357, 237));
        points.add(new Point(357, 338));
        points.add(new Point(202, 338));
        sections.add(points);
        line = new GuiLine(sections, UEXT.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(191, 349));
        points.add(new Point(191, 343));
        sections.add(points);
        line = new GuiLine(sections, Oper1.UEXTout1());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(181, 338));
        points.add(new Point(109, 338));
        sections.add(points);
        line = new GuiLine(sections, TSBUEXTout1.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(109, 393));
        points.add(new Point(357, 393));
        points.add(new Point(357, 411));
        sections.add(points);
        line = new GuiLine(sections, IBUS1low.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(275, 422));
        points.add(new Point(283, 422));
        sections.add(points);
        line = new GuiLine(sections, LogicalComponent.getCPUCLK());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(275, 441));
        points.add(new Point(283, 441));
        sections.add(points);
        line = new GuiLine(sections, Oper1.ldBR());
        gui.addLine(line);


        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(357, 450));
        points.add(new Point(357, 483));
        sections.add(points);
        line = new GuiLine(sections, BR.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(297, 495));
        points.add(new Point(297, 502));
        sections.add(points);
        line = new GuiLine(sections, new Pin(false));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(327, 495));
        points.add(new Point(327, 502));
        sections.add(points);
        line = new GuiLine(sections, new Pin(false));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(342, 495));
        points.add(new Point(342, 502));
        sections.add(points);
        line = new GuiLine(sections, BRbits.getOut(7));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(372, 495));
        points.add(new Point(372, 502));
        sections.add(points);
        line = new GuiLine(sections, BRbits.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(387, 495));
        points.add(new Point(387, 502));
        sections.add(points);
        line = new GuiLine(sections, new Pin(false));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(331, 524));
        points.add(new Point(331, 551));
        points.add(new Point(443, 551));
        sections.add(points);
        line = new GuiLine(sections, IVTDSP.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(455, 562));
        points.add(new Point(455, 555));
        sections.add(points);
        line = new GuiLine(sections, Oper1.IVTDSPout2());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(464, 551));
        points.add(new Point(541, 551));
        sections.add(points);
        line = new GuiLine(sections, TSBIVTDSPout2.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(541, 596));
        points.add(new Point(357, 596));
        points.add(new Point(357, 618));
        sections.add(points);
        line = new GuiLine(sections, Bus1.IBUS2());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(275, 628));
        points.add(new Point(282, 628));
        sections.add(points);
        line = new GuiLine(sections, LogicalComponent.getCPUCLK());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(275, 646));
        points.add(new Point(282, 646));
        sections.add(points);
        line = new GuiLine(sections, Oper1.ldIVTP());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(357, 656));
        points.add(new Point(357, 678));
        points.add(new Point(202, 678));
        sections.add(points);
        line = new GuiLine(sections, IVTP.getOut(0));
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(191, 689));
        points.add(new Point(191, 683));
        sections.add(points);
        line = new GuiLine(sections, Oper1.IVTPout1());
        gui.addLine(line);

        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(180, 678));
        points.add(new Point(109, 678));
        sections.add(points);
        line = new GuiLine(sections, TSBIVTPout1.getOut(0));
        gui.addLine(line);

        // LABELE:
        gui.addLabel(new GuiLabel(112, 60, Bus1.IBUS1()));
        gui.addLabel(new GuiLabel(112, 630, Bus1.IBUS1()));
        gui.addLabel(new GuiLabel(548, 60, Bus1.IBUS2()));
        gui.addLabel(new GuiLabel(548, 630, Bus1.IBUS2()));
        gui.addLabel(new GuiLabel(350, 59, UINT.getOut(0)));
        gui.addLabel(new GuiLabel(130, 163, TSBUINTout1.getOut(0)));
        gui.addLabel(new GuiLabel(350, 232, UEXT.getOut(0)));
        gui.addLabel(new GuiLabel(130, 351, TSBUEXTout1.getOut(0)));
        gui.addLabel(new GuiLabel(366, 405, IBUS1low.getOut(0)));
        gui.addLabel(new GuiLabel(366, 480, BR.getOut(0)));
        gui.addLabel(new GuiLabel(331, 564, IVTDSP.getOut(0)));
        gui.addLabel(new GuiLabel(366, 610, Bus1.IBUS2()));
        gui.addLabel(new GuiLabel(366, 683, IVTP.getOut(0)));
        gui.addLabel(new GuiLabel(130, 691, TSBIVTPout1.getOut(0)));
    }

    public static Pin UINT() {
        return UINT.getOut(0);
    }

    public static Pin UEXT() {
        return UEXT.getOut(0);
    }

    public static Pin BR() {
        return BR.getOut(0);
    }

    public static Pin IVTP() {
        return IVTP.getOut(0);
    }

    public static Pin IVTDSP() {
        return IVTDSP.getOut(0);
    }

    public static Pin IVTPout() {
        return TSBIVTPout1.getOut(0);
    }

    public static Pin IVTDSPout() {
        return TSBIVTDSPout2.getOut(0);
    }

    public static REG RegBR() {
        return BR;
    }

    public static REG RegIVTP() {
        return IVTP;
    }
}
